package com.jrwd.recycletest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * 相信自己，创造未来
 *
 * @author 小兵
 * @date 2017/10/21
 */

public class TopView extends RelativeLayout {

    private Context context;
    private ImageView ivBack;
    private TextView tvTitle;
    private TextView tvRightContent;
    private ImageView ivRight;
    private ImageView ivClose;

    public TopView(Context context) {
        super(context);
        this.context=context;
        initView();
    }
    public TopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        initView();
    }
    public TopView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context=context;
        initView();
    }

    private void initView() {

        setFitsSystemWindows(true);
        setClipToPadding(true);
        LayoutInflater.from(context).inflate(R.layout.layout_top_view, this, true);
        ivBack= (ImageView) this.findViewById(R.id.iv_back);
        tvTitle= (TextView) this.findViewById(R.id.tv_title);
        tvRightContent= (TextView) this.findViewById(R.id.tv_right_content);
        ivRight = (ImageView) this.findViewById(R.id.iv_right);
        ivClose= (ImageView) this.findViewById(R.id.iv_close);
    }

    public void init(boolean isBack,int title,int rightContent,final OnClickTopListener onClickTopListener){

        tvTitle.setText(context.getString(title));
        if (isBack){
            ivBack.setVisibility(View.VISIBLE);
        }else {
            ivBack.setVisibility(View.GONE);
        }
        if (rightContent!=0){
            tvRightContent.setText(context.getString(rightContent));
        }
        if (onClickTopListener!=null){
            ivBack.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickTopListener.onLeft();
                }
            });
            tvRightContent.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickTopListener.onRight();
                }
            });
            ivRight.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickTopListener.onRight();
                }
            });
            ivClose.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickTopListener.onClose();
                }
            });
        }
    }

    public void setTitleSize(float size) {
        tvTitle.setTextSize(size);
    }

    public void setTitleColor(int resId) {
        tvTitle.setTextColor(getResources().getColor(resId));
    }

    public void setTitleText(String text) {
        tvTitle.setText(text);
    }

    public void setRightSize(float size) {
        tvRightContent.setTextSize(size);
    }

    public void setRightColor(int resId) {
        tvRightContent.setTextColor(getResources().getColor(resId));
    }

    public void setRightText(String text) {
        tvRightContent.setText(text);
    }

    public void setRigthImage(int resId){
        tvRightContent.setVisibility(View.GONE);
        ivRight.setImageResource(resId);
        ivRight.setVisibility(View.VISIBLE);
    }

    public void setRightTextClickable(boolean flag){
        tvRightContent.setClickable(flag);
    }

    public void setCloseVisvibe(int visvibe){
        ivClose.setVisibility(visvibe);
    }

    public static abstract class OnClickTopListener {
        public void onLeft() {}
        public void onTitle() {}
        public void onRight() {}
        public void onClose(){}
    }
    public void setTvRightContenteVisvibe(int visvibe){
        tvRightContent.setVisibility(visvibe);
    }
}
